package com.wgts.wgts.mvp.view.activity.homapge;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerHomePageTimeActionFragmentComponent;
import com.wgts.wgts.dagger.module.HomePageTimeActionFragmentModule;
import com.wgts.wgts.mvp.presenter.IHomePageTimeActionFragmentPresenter;
import com.wgts.wgts.mvp.view.IHomePageTimeActionFragmentView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.fragment.HomePageContentFragment;
import com.wgts.wgts.netmodle.resp.ActivityListEntity;
import com.wgts.wgts.netmodle.resp.TimeProductEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomePageTimeActionFragment extends LazyBaseFragment implements IHomePageTimeActionFragmentView {

    private static final String BUNDLE = "BUNDLE";
    @BindView(R.id.load_ly)
    LinearLayout loadLy;
    private boolean noMore;

    @BindView(R.id.wrap_recycle_view)
    RecyclerView wrapRecycleView;
    //    @BindView(R.id.time_refresh)
//    SmartRefreshLayout timeRefresh;
    Unbinder unbinder;

    @Inject
    IHomePageTimeActionFragmentPresenter presenter;

    private ActivityListEntity entity;

    private List<TimeProductEntity.Entity> entities;

    private HomePageTimeActionAdapter adapter;


    private CustomLineaLayoutManager linearLayoutManager;

    private static HomePageContentFragment homePageContent;

    @Override
    public void init() {
        entity = getArguments().getParcelable(BUNDLE);
        DaggerHomePageTimeActionFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .homePageTimeActionFragmentModule(new HomePageTimeActionFragmentModule(this))
                .build()
                .inject(this);

        entities = new ArrayList<>();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_page_item, container, false);

        unbinder = ButterKnife.bind(this, view);

        initView();
        return view;
    }

    @Override
    protected void loadData() {
        presenter.timeProduct(entity.id, entity.beginTime, entity.endTime);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);


    }


    private void initView() {
        linearLayoutManager = new CustomLineaLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrapRecycleView.setLayoutManager(linearLayoutManager);

        adapter = new HomePageTimeActionAdapter(getContext(), entities);

        adapter.setOnclickListener(new HomePageTimeActionAdapter.OnclickListener() {
            @Override
            public void onClick(View v, int postion) {
                Intent intent = new Intent(getContext(), JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.ACTION_PRODUCT);

                switch (entities.get(postion).status) {
                    case "PENDING":
                        intent.putExtra(JDGoodinfoActivity.ACTION_STATE, JDGoodinfoActivity.PENDING);
                        break;
                    case "ACTIVE":
                        intent.putExtra(JDGoodinfoActivity.ACTION_STATE, JDGoodinfoActivity.ACTIVE);
                        break;
                    case "DELETED":
                        intent.putExtra(JDGoodinfoActivity.ACTION_STATE, JDGoodinfoActivity.DELETED);
                        break;
                }

                intent.putExtra("id", String.valueOf(entities.get(postion).productId));
                intent.putExtra(JDGoodinfoActivity.ACTIVITYID, entity.id);
                intent.putExtra(JDGoodinfoActivity.BEGINTIME, entity.beginTime);
                intent.putExtra(JDGoodinfoActivity.ENDTIME, entity.endTime);
                getActivity().startActivity(intent);
            }
        });

        wrapRecycleView.setAdapter(adapter);
        loadMore();
    }

    public static Fragment getInstance(ActivityListEntity entity, HomePageContentFragment homePageContentFragment) {
        Fragment fragment = new HomePageTimeActionFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(BUNDLE, entity);
        fragment.setArguments(bundle);
        homePageContent = homePageContentFragment;
        return fragment;
    }


    @Override
    public void initTimeProduct(TimeProductEntity timeProductEntity, boolean noMore) {
        entities.clear();
        entities.addAll(timeProductEntity.list);
        this.noMore = noMore;
        adapter.setNoMore(noMore);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void moreTimeProduct(TimeProductEntity timeProductEntity, boolean noMore) {
        loadLy.setVisibility(View.GONE);
        entities.addAll(timeProductEntity.list);
        adapter.setOnclickListener(new HomePageTimeActionAdapter.OnclickListener() {
            @Override
            public void onClick(View v, int postion) {
                Intent intent = new Intent(getContext(), JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra("id", String.valueOf(entities.get(postion).productId));
                getActivity().startActivity(intent);
            }
        });
        adapter.setNoMore(noMore);
        this.noMore = noMore;
        adapter.notifyDataSetChanged();

    }

    private void loadMore() {
        wrapRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!recyclerView.canScrollVertically(1)) {
                    if (!noMore) {
                        loadLy.setVisibility(View.VISIBLE);
                        presenter.moreTimeProdct();
                        noMore = true;
                    }
                }
            }
        });
    }


    @Override
    public void onError(int status, String msg) {
        loadLy.setVisibility(View.GONE);
    }
}
